package com.cn.demo4.dao.impl;

import com.cn.demo4.dao.CompanyDao;
import com.cn.demo4.model.CompanyModel;
import com.cn.demo4.model.PageModel;
import com.cn.demo4.util.GetSqlSession;
import com.cn.demo4.util.JDBC;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

/**
 * @author Administrator
 */
public class CompanyDaoImpl implements CompanyDao {
//    @Override
//    public List<CompanyModel> findAll(CompanyModel companyModel) {
//        String sql = "select * from company order by id desc";
//        StringBuilder sql = new StringBuilder();
//        sql.append(" select * from company where true");
//        if (!"".equals(companyModel.getName())) {
//            sql.append(" and name like '%").append(companyModel.getName()).append("%'");
//        }
//        if (!"".equals(companyModel.getPhone())) {
//            sql.append(" and phone like '%").append(companyModel.getPhone()).append("%'");
//        }
//        if (!"".equals(companyModel.getAddress())) {
//            sql.append(" and address like '%").append(companyModel.getAddress()).append("%'");
//        }
//        if (companyModel.getStatus() != -1) {
//            if (companyModel.getStatus() == 1) {
//                sql.append(" and status=1");
//            } else {
//                sql.append(" and status is null and status=0");
//            }
//        }
//        sql.append(" order by id desc");
//        sql.append(" limit ").append((companyModel.getPageModel().getCurPage() - 1) * companyModel.getPageModel().getPageSize()).append(",").append(companyModel.getPageModel().getPageSize());
//        ResultSet resultSet = JDBC.select(sql.toString());
//        return getData(resultSet);
//        return GetSqlSession.getSqlSession().selectList("com.cn.demo4.dao.CompanyDao.findAll", companyModel);
//    }

    @Override
    public int add(CompanyModel companyModel) {
//        String sql = "insert into company(name,logo,phone,email,online,address,content," +
//                "qrcode_index,qrcode_weixin,qrcode_index_title,qrcode_weixin_title," +
//                "record_number,record_message,status,email_img,phone_img,address_img,consulting_center,create_time,update_time)" +
//                " values('" + companyModel.getName() + "','" + companyModel.getLogo() + "'," +
//                "'" + companyModel.getPhone() + "','" + companyModel.getEmail() + "'," +
//                "'" + companyModel.getOnline() + "'," +
//                "'" + companyModel.getAddress() + "','" + companyModel.getContent() + "'," +
//                "'" + companyModel.getQrCodeIndex() + "','" + companyModel.getQrCodeWeiXin() + "'," +
//                "'" + companyModel.getQrCodeIndexTitle() + "','" + companyModel.getQrCodeWeiXinTitle() + "'," +
//                "'" + companyModel.getRecordNumber() + "','" + companyModel.getRecordMessage() + "'," +
//                "" + companyModel.getStatus() + "," +
//                "'" + companyModel.getEmailImg() + "','" + companyModel.getPhoneImg() + "','" + companyModel.getAddressImg() + "'," +
//                "'" + companyModel.getConsultingCenter() + "'," +
//                "now(),now())";
//        return JDBC.upDate(sql);
        return GetSqlSession.getSqlSession().insert("com.cn.demo4.dao.CompanyDao.add",companyModel);
    }

    @Override
    public CompanyModel findById(Integer id) {
//        String sql = "select * from company where id=" + id;
//        ResultSet resultSet = JDBC.select(sql);
//        List<CompanyModel> list = getData(resultSet);

        return GetSqlSession.getSqlSession().selectOne("com.cn.demo4.dao.CompanyDao.findById",id);
    }

    @Override
    public Integer update(CompanyModel companyModel) {
//        String sql = "update company set name='" + companyModel.getName() + "',";
//        if (companyModel.getLogo() != null && !companyModel.getLogo().equals("undefined")) {
//            sql += "logo='" + companyModel.getLogo() + "',";
//        }
//        sql += " phone='" + companyModel.getPhone() + "',email='" + companyModel.getEmail() + "',content='" + companyModel.getContent() + "',";
//        if (companyModel.getQrCodeIndex() != null && !companyModel.getQrCodeIndex().equals("undefined")) {
//            sql += " qrcode_index='" + companyModel.getQrCodeIndex() + "',";
//        }
//        if (companyModel.getQrCodeWeiXin() != null && !companyModel.getQrCodeWeiXin().equals("undefined")) {
//            sql += "qrcode_weixin='" + companyModel.getQrCodeWeiXin() + "',";
//        }
//
//        sql += " qrcode_index_title='" + companyModel.getQrCodeIndexTitle() + "',qrcode_weixin_title='" + companyModel.getQrCodeWeiXinTitle() + "'," +
//                " record_number='" + companyModel.getRecordNumber() + "',record_message='" + companyModel.getRecordMessage() + "'," +
//                " status=" + companyModel.getStatus() + ",address='" + companyModel.getAddress() + "',online='" + companyModel.getOnline() + "'," +
////                " email_img='"+companyModel.getEmailImg()+"',phone_img='"+companyModel.getPhoneImg()+"',address_img='"+companyModel.getAddressImg()+"'," +
////                " consulting_center='"+companyModel.getConsultingCenter()+"',"+
//                " update_time=now() where id=" + companyModel.getId();
//        return JDBC.upDate(sql);
        return GetSqlSession.getSqlSession().update("com.cn.demo4.dao.CompanyDao.update",companyModel);
    }

    @Override
    public int del(Integer id) {
//        String sql = "delete from company where id=" + id;
//        return JDBC.upDate(sql);
        return GetSqlSession.getSqlSession().delete("com.cn.demo4.dao.CompanyDao.del",id);
    }

    /**
     * 前台servlet得到数据
     *
     * @return
     */
    @Override
    public CompanyModel getCompanyModelListForHome() {
        return GetSqlSession.getSqlSession().selectOne("com.cn.demo4.dao.CompanyDao.getCompanyModelListForHome");
    }

    @Override
    public int getCount(CompanyModel companyModel) {
        return GetSqlSession.getSqlSession().selectOne("com.cn.demo4.dao.CompanyDao.getCount",companyModel);

    }

    @Override
    public CompanyModel findAll(CompanyModel companyModel) {
        return GetSqlSession.getSqlSession().selectOne("com.cn.demo4.dao.CompanyDao.findAll", companyModel);
    }

    private List<CompanyModel> getData(ResultSet resultSet) {
        List<CompanyModel> list = new ArrayList<>();
        try {
            while (resultSet.next()) {
                CompanyModel companyModel = new CompanyModel();
                companyModel.setId(resultSet.getInt("id"));
                companyModel.setName(resultSet.getString("name"));
                companyModel.setContent(resultSet.getString("content"));
                companyModel.setPhone(resultSet.getString("phone"));
                companyModel.setEmail(resultSet.getString("email"));
                companyModel.setLogo(resultSet.getString("logo"));
                companyModel.setOnline(resultSet.getString("online"));
                companyModel.setAddress(resultSet.getString("address"));
                companyModel.setQrCodeIndex(resultSet.getString("qrcode_index"));
                companyModel.setQrCodeWeiXin(resultSet.getString("qrcode_weixin"));
                companyModel.setQrCodeIndexTitle(resultSet.getString("qrcode_index_title"));
                companyModel.setQrCodeWeiXinTitle(resultSet.getString("qrcode_weixin_title"));
                companyModel.setRecordMessage(resultSet.getString("record_message"));
                companyModel.setRecordNumber(resultSet.getString("record_number"));
                companyModel.setStatus(resultSet.getInt("status"));
                companyModel.setCreateTime(resultSet.getString("create_time"));
                companyModel.setUpdateTime(resultSet.getString("update_time"));
                companyModel.setEmailImg(resultSet.getString("email_img"));
                companyModel.setPhoneImg(resultSet.getString("phone_img"));
                companyModel.setAddressImg(resultSet.getString("address_img"));
                companyModel.setConsultingCenter(resultSet.getString("consulting_center"));
                companyModel.setNickName(resultSet.getString("nickName"));
                companyModel.setTag(resultSet.getString("tag"));
                list.add(companyModel);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return list;
    }
}
